export const formatTime = (date: Date) => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()
  console.log(year);

  return (
    [year, month, day].map(formatNumber).join('/') +
    ' ' +
    [hour, minute, second].map(formatNumber).join(':')
  )
}

const formatNumber = (n: number) => {
  const s = n.toString()
  return s[1] ? s : '0' + s
}

// px 转换为 rpx ，传参类型是数字（Number）
export const pxTorpx = (px: number) => {
  let deviceWidth = wx.getSystemInfoSync().windowWidth; //获取设备屏幕宽度
  let rpx = (750 / deviceWidth) * Number(px)
  return Math.floor(rpx);
}

// rpx 转换为 px ，传参类型是数字（Number）
export function rpxTopx(rpx: number) {
  let deviceWidth = wx.getSystemInfoSync().windowWidth; //获取设备屏幕宽度
  let px = (deviceWidth / 750) * Number(rpx)
  return Math.floor(px);
}

// 根据tab选择页获取分类标识
const categoryMap = {
  "企业号": {
    category: 2,
    carouselCategory:16
  },
  "精彩推荐": {
    category: 1,
    carouselCategory: 9
  },
  "文旅": {
    category: 3,
    carouselCategory: 10
  },
  "新农业": {
    category: 4,
    carouselCategory: 11
  },
  "生活": {
    category: 5,
    carouselCategory: 12
  },
  "地产": {
    category: 6,
    carouselCategory: 13
  },
  "制造": {
    category: 7,
    carouselCategory: 14
  },
  "短视频": {
    category: 8,
    carouselCategory: 15
  },
  "教育":{
    
  }

}
export function getCategory(tabName: string) {
  return categoryMap[tabName]
}

// 获取两个坐标点之间的距离
export function getDistance([lat1, lng1]: Array<any>, [lat2, lng2]: Array<any>) {
  lat1 = lat1 || 0;
  lng1 = lng1 || 0;
  lat2 = lat2 || 0;
  lng2 = lng2 || 0;

  var rad1 = lat1 * Math.PI / 180.0;
  var rad2 = lat2 * Math.PI / 180.0;
  var a = rad1 - rad2;
  var b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
  var r = 6378137;
  var distance = r * 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(rad1) * Math.cos(rad2) * Math.pow(Math.sin(b / 2), 2)));

  return distance;
}

// 获取video节点
export const getVideoContext = (id, instance) => {
  const query = wx.createSelectorQuery().in(instance);
  const queryVideo = query.select(`#${id}`).context();
  return new Promise((resolve, reject) => {
    queryVideo.exec(res => {
      resolve(res);
    });
  });
}
// 获取canvas
export const getCanvasNode = (id, instance) => {
  const query = wx.createSelectorQuery().in(instance);
  const queryCvs = query.select(`#${id}`).node();
  
  return new Promise((resolve, reject) => {
    queryCvs.exec(res => {
      resolve(res);
    });
  });
}